import {
    revokeDispatch,
    getDispatchExamine,
    deleteDispatch,
    getOccupationApplyList,
    deleteOccupationById
} from '../../api/long-insure'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        occupationId: '',
        showDelete: false,
        showWithdraw: false,
        detail: {},
        statusMap: {
            0: '待审核',
            1: '已通过', // 正常 页面不显示
            2: '已驳回',
            3: '已禁用'
        },
        applyList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (options.id) {
            this.setData({
                id: options.id
            })
            this.getDispatchExamine(options.id)
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    /**
     * 显示删除弹框
     */
    onShowDelete() {
        this.setData({
            showDelete: true
        })
    },
    /**
     * 隐藏删除弹框
     */
    onHideDelete() {
        this.setData({
            showDelete: false
        })
    },
    /**
     * 点击确认撤销
     */
    onDelete() {
        // to do

        this.onWithdrawCancel()
    },
    onAdd() {
        wx.navigateTo({
            url: `/packageLongInsure/pages/dispatch-add/dispatch-add?id=${this.data.detail.id}&addType=apply`,
        })
    },
    /**
     * 申请记录列表
     */
    async getOccupationApplyList() {
        const res = await getOccupationApplyList({
            periodServeCompanyId: this.data.id,
            isDelete: 0
        })
        console.log(21213333, res)
        if (res?.length) {
            res.forEach(i => {
                if (i.fileDetails) {
                    i.fileList = i.fileDetails.filter(i => i.fileUrl)
                }
            })
        }
        this.setData({
            applyList: res || []
        })
        console.log(111, this.data.applyList)
    },
    /**
     * 获取详情
     * @param {*} id number
     */
    async getDispatchExamine(id) {
        const result = await getDispatchExamine({
            id: id
        })
        console.log(3333333, result)
        if (result.status === 0 || result.status === 2) {
            let data = {
                createUserName: result.createUserName,
                fileList: result.fileDetailList,
                createTime: result.updateTime || result.createTime,
                status: result.status,
                rejectRemark: result.rejectRemark
            }
            console.log(3333, data)
            this.setData({
                applyList: [data]
            })
        } else {
            this.getOccupationApplyList()
        }
        this.setData({
            detail: result
        })
        console.log(111, result)
    },
    /**
     * 点击查看视频
     * @param {*} ev 
     */
    onWatchVideo() {
        if (!this.data.detail.fileDetailList?.length) {
            app.toast('暂无视频')
            return
        }
        const videos = this.data.detail.fileDetailList.map(i => {
            return {
                url: i,
                type: 'video'
            }
        })
        wx.previewMedia({
            sources: videos
        })
    },
    onWatchVideoc(e) {
        console.log(e, 'gsduhfghu')
        let arr = e.currentTarget.dataset.filedetaillist
        if (!arr?.length) {
            app.toast('暂无视频')
            return
        }
        const videos = arr.map(i => {
            return {
                url: i.fileUrl,
                type: 'video'
            }
        })
        wx.previewMedia({
            sources: videos
        })
    },
    /**
     * 点击撤销申请
     */
    onShowWithDraw() {
        this.setData({
            showWithdraw: true
        })
    },
    /**
     * 点击取消撤销申请
     */
    onHideWithDraw() {
        this.setData({
            showWithdraw: false
        })
    },
    /**
     * 撤销
     * 企业审核状态为待审核时撤销的是企业申请 否则撤销的是新工种
     */
    async onHandleWithdraw() {
        if (this.data.detail.status === 0) {
            await revokeDispatch({
                id: this.data.detail.id
            })
            app.toast('撤销成功')
            this.onHideWithDraw()
            wx.navigateBack()
        } else {
            await deleteOccupationById(this.data.applyList[0].id)
            app.toast('撤销成功')
            this.onHideWithDraw()
            this.getOccupationApplyList()
        }
    },
    /**
     * 删除企业
     */
    async onHandleDelete() {
        await deleteDispatch({
            id: this.data.detail.id
        })
        app.toast('删除被派遣单位成功')
        setTimeout(() => {
            wx.navigateBack()
        }, 0)
    }
})